package com.jaws.demo.controller;

import io.swagger.annotations.ApiOperation;

import java.io.File;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jaws.core.common.excel.domain.JawsExcelTemplate;
import com.jaws.core.common.util.MockUtil;
import com.jaws.core.mvc.view.JawsExcelView;
import com.jaws.demo.vo.Order;
import com.jaws.demo.vo.Plan;

@Controller
public class ExcelController {

	private static final String basePath = ExcelController.class.getResource("/").getPath();

	@ApiOperation(value = "测试excel导出接口")
	@RequestMapping(value = "/excel")
	public String testExcel(ModelAndView model) {
		// 1. 获取导出数据
		Map<String, Object> rootObjectMap = Maps.newHashMap();
		Order order1 = null;
		try {
			order1 = MockUtil.randomInstanceOfNonCollection(Order.class);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<Order> list = Lists.newArrayList(order1);

		// 2. 封装JawsExcelTemplate，填充数据，excel模板路径，导出文件名
		rootObjectMap.put("list", list);
		JawsExcelTemplate template = new JawsExcelTemplate();
		template.setData(rootObjectMap);
		template.setTemplatePath(basePath + "template" +  File.separator + "01_Template_foreach.xlsx" );
		template.setFilename("mjaaa");

		// 3. 将JawsExcelTemplate添加到spring ModelAndView中，返回jawsExcelView视图
		model.getModelMap().addAttribute(JawsExcelView.TEMPLATE, template);
		return JawsExcelView.VIEW;
	}

	@ApiOperation(value = "测试excel2导出接口")
	@RequestMapping(value = "/excel2")
	public String testExcel2(ModelAndView model) {
		// 1. 获取导出数据
		Map<String, Object> rootObjectMap = Maps.newHashMap();
		Order order1 = null;
		Order order2 = null;
		Plan plan = null;
		List<Order> list = null;
		try {
			plan = MockUtil.randomInstanceOfNonCollection(Plan.class);
			order1 = MockUtil.randomInstanceOfNonCollection(Order.class);
			order2 = MockUtil.randomInstanceOfNonCollection(Order.class);
			list = Lists.newArrayList(order1, order2);
		} catch (Exception e) {
			e.printStackTrace();
		}

		// 2. 封装JawsExcelTemplate，填充数据，excel模板路径，导出文件名
		rootObjectMap.put("mangerName", "张三");
		rootObjectMap.put("plan", plan);
		rootObjectMap.put("list", list);
		JawsExcelTemplate template = new JawsExcelTemplate();
		template.setData(rootObjectMap);
		template.setFilename("测试混合模板");
		template.setTemplatePath(basePath + "template" +  File.separator + "03_Template_混合模板.xlsx" );

		// 3. 将JawsExcelTemplate添加到spring ModelAndView中，返回jawsExcelView视图
		model.getModelMap().addAttribute(JawsExcelView.TEMPLATE, template);
		return JawsExcelView.VIEW;
	}

}
